<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class AdminUsers extends Model
{
    protected $table = 'admin_users';

    protected $primaryKey = 'id';

    public $timestamps = true;

    public function roles()
    {
        return $this->belongsToMany('App\Model\Role');
    }

    /**
     * 查找某个用户有的权限
     * @param  [int] $id [这个用户的ID]
     * @return [arr]     [这个用户的权限]
     */
    public function haspermission($id)
    {
        
    	//登录用户的模型
        $usermod = AdminUsers::find($id);

        //登录用户对应的角色
        $roles = $usermod->roles;

        //登录用户对应的角色再对应的权限
        $permission = [];

        foreach ($roles as $key => $value) {
            $permission = $value;
        }

        //用来放权限的容器
        $over = [];

        foreach ($roles as $value) {
            $over[] = $value->permissions->toArray();
        }

        //获取改用户的权限 arr
        $permission_list = [];
        foreach ($over as $value) {
            foreach ($value as $perm) {
                $permission_list[] = $perm['name'];
            }
        }

        return $permission_list;
    }
}
